package com.llynic.designdemo.responsibilitymode;


import java.util.ArrayList;
import java.util.List;

public class ResponsibilityChain {

    private List<Responsibility> responsibilitys = new ArrayList<>();

    private int index = 0;

    public ResponsibilityChain() {
    }

    public ResponsibilityChain(List<Responsibility> responsibilitys) {
        this.responsibilitys = responsibilitys;
    }

    //执行下一个责任
    public void process(Request request){
        if (this.index < responsibilitys.size()){
            responsibilitys.get(index++).process(request,this);
        }
    }

    //注册新的责任
    public void register(Responsibility responsibility){
        this.responsibilitys.add(responsibility);
    }
}
